<?php
/**
 * 基础接口
 * @author shitou
 *
 */
class LocalUpload {
	/**
     * 生产token
     * @access public
     * @return array
     */
	public function createToken(){
		return '';
    }
	
    /**
     * 创建文件夹
     * @access public
     * @return array
     */
	public function createFolder($dir){
		if(file_exists($dir)){
			return 2;
		}
		if(is_dir($dir)){
			return 3;
		}
		if(mkdir($dir)){
			return 1;
		}else{
			return 4;
		}
    }
	
	/**
     * 删除文件夹
     * @access public
     * @return array
     */
	public function deleteFolder($folderkey){
		$dir = ABS_PATH.UPLOAD_PATH.$folderkey;
		$dir = folderFilter($dir);
		del_dir($dir);
    }
	
	/**
     * 文件路径
     * @access public
     * @return string
     */
	public function uploadFile($arr){
		$Upload_folder = M("UploadFolder");
		$record = $Upload_folder->where('id='.$arr['folder_id'])->find();
		$arr['folderkey'] = $record['folderkey'];
		
		$dir = ABS_PATH.UPLOAD_PATH.$arr['folderkey'];
		$dir = folderFilter($dir);
		
		import('ORG.Net.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下  
		$upload=new UploadFile();  
		$upload->maxSize=C('FL_maxSize');//默认为-1，不限制上传大小  
		$upload->savePath=$dir;//保存路径建议与主文件平级目录或者平级目录的子目录来保存     
		$upload->saveRule=uniqid;//上传文件的文件名保存规则  
		$upload->uploadReplace=true;//如果存在同名文件是否进行覆盖  
		$upload->allowExts=C('FL_allowExts');//准许上传的文件类型  
		//$upload->allowTypes=array('image/png','image/jpg','image/jpeg','image/gif');//检测mime类型  
		$upload->thumb=true;//是否开启图片文件缩略图  
		$upload->thumbMaxWidth=C('FL_thumbMaxWidth');  
		$upload->thumbMaxHeight=C('FL_thumbMaxHeight');  
		$upload->thumbPrefix='';//缩略图文件前缀
		$upload->thumbSuffix=C('FL_thumbSuffix');//缩略图文件后缀  
		$upload->thumbRemoveOrigin=0;//如果生成缩略图，是否删除原图
		
		if($upload->upload()){
			$info=$upload->getUploadFileInfo();
			
			$result['status'] = 1;
			$result['info'] = '上传成功';
			$result['file'] = $info;
		}else{ 
			$result['status'] = 0;
			$result['info'] = $upload->getErrorMsg();   
			$result['file'] = ''; 
		}
		
		$lastFileId = 0;
		$lastFileKey = '';
		foreach($result['file'] as $v){
			$Upload_file = M("UploadFile");
			$Upload_file->filetitle = $v['name'];
			$Upload_file->folder_id = $arr['folder_id'];
			$lastFileKey = $arr['folderkey'].$v['savename'];
			$Upload_file->filekey = $lastFileKey;
			$Upload_file->filetype = $v['extension'];
			$Upload_file->addid = $arr['addid'];
			$Upload_file->addtime = time();
			$lastFileId = $Upload_file->add();
		}
		
		session('lastFileKey',$lastFileKey);
		
		return $result;
	}
	
	/**
     * 删除文件
     * @access public
     * @return array
     */
	public function deleteFile($filekey){
		$filepath = folderFilter(ABS_PATH.UPLOAD_PATH.$filekey);
		$filearr = explode('.',$filepath);
		
		@unlink($filepath);
		
		if(in_array($filearr[1],C('FL_imageExts'))){	
			$filethumb = explode(',',C('FL_thumbSuffix'));
			foreach($filethumb as $k){
				$filepath_s = $filearr[0].$k.'.'.$filearr[1];
				@unlink($filepath_s);
			}
		}
    }
	
	/**
     * 文件路径
     * @access public
     * @return string
     */
	public function fileUrl($filekey,$thumb='',$integral=true){
		$fileurl = '';
		
		if($integral){
			$filepath = folderFilter(ABS_URL.UPLOAD_PATH.$filekey,true);
		}else{
			$filepath = folderFilter($filekey,true);
		}
				
		if(empty($thumb)){
			$fileurl = $filepath;			
		}else{
			$filearr = explode('.',$filepath);
			$fileext = $filearr[count($filearr)-1];
			$filename = str_replace('.'.$fileext,'',$filepath);
			$fileurl = $filename.$thumb.'.'.$fileext;
		}
		
		return $fileurl;
	}
}
?>